<?php
/**
 * Created by PhpStorm.
 * HealthySample: 28618
 * Date: 2020/2/26
 * Time: 11:07
 */
namespace App\Observers;

use App\Http\Models\HealthySample;
use App\Utils\OtherUtil;
use Illuminate\Support\Facades\Auth;
use DB;
class HealthySampleObserver
{
    static $num=0;
    public function creating(HealthySample $HealthySample)
    {
        $this->getHealhtySampleState($HealthySample);
    }
    /**
     * @param HealthySample $HealthySample
     */
    public function updating(HealthySample $HealthySample)
    {
        if ($HealthySample->isDirty('healthy_sample_status')) {
            $healthy_sample_status = $HealthySample->healthy_sample_status;
            $healthy_collector_number = $HealthySample->healthy_collector_number;
            $chain_id = $HealthySample->id;
            $user = DB::select('select * from health.healthy_samples where healthy_collector_number = :healthy_collector_number', [':healthy_collector_number' => $healthy_collector_number]);
            $userId = $user[0]->created_uid;
            $stateArray = $this->getHealhtySampleState($healthy_sample_status);
            $param = array(
                'user_id' => $userId,
                'type' => 3,
                'title' => $stateArray['title'],
                'notice_content' => $stateArray['message_notice'],
                'chain_id' => $chain_id
            );
            $query = $this->getDoInsetSql($param);
            if(self::$num==0){
                DB::insert($query);
            }
            self::$num= self::$num+1;
        }
    }


    /**
     * @param HealthySample $HealthySample
     */
    public function deleting(HealthySample $HealthySample)
    {
    }

    /**
     * @param HealthySample $HealthySample
     * @return void
     */
    public function restoring(HealthySample $HealthySample)
    {
    }

    /**
     * @param HealthySample $HealthySample
     * @return void
     */
    public function forceDeleted(HealthySample $HealthySample)
    {
    }

    public function updated(HealthySample $HealthySample){


    }
    /**
     * @param HealthySample $HealthySample
     * @return void
     */
    public function getHealhtySampleState($healthy_sample_status)
    {
        switch ($healthy_sample_status) {
            case 2:
                $title = '采集器绑定完成';
                $message_notice = '您的样本信息绑定成功，为保证您的样本检测进度，请尽快安排完成采样并通过顺丰快递回寄样本！';
                break;
            case 3:
                $title = '样本回寄完成';
                $message_notice = '样本回寄完成，请您耐心等待，及时关注app信息。';
                break;
            case 4:
                $title = '样本接收成功';
                $message_notice = '收到您的样本，我们将尽快安排实验。';
                break;
            case 5:
                $title = 'DNA提取进行中';
                $message_notice = '样本正在进行DNA提取，请您耐心等待！';
                break;
            case 6:
                $title = 'DNA提取完成';
                $message_notice = 'DNA提取完成，即将进行建库';
                break;
            case 7:
                $title = '样本上机检测中';
                $message_notice = '样本正在上机检测，此过程将持续大约3天，请您耐心等待！';
                break;
            case 8:
                $title = '样本数据分析中';
                $message_notice = '';
                break;
            case 11:
                $title = '报告已签发';
                $message_notice = '您的报告已经签发，请查看！';
                break;
        }
        $stateArray = array('title'=> $title,'message_notice'=>$message_notice);
        return    $stateArray;
    }

    public function getDoInsetSql($param)
    {
        $user_id = $param['user_id'];
        $type = $param['type'];
        $title = $param['title'];
        $notice_content = $param['notice_content'];
        $chain_id = $param['chain_id'];
        $create_time = date('Y-m-d H:i:s', time());
        $sql = "INSERT INTO app.message_notice(user_id, type, create_time, title, notice_content, is_read, chain_id, read_time) VALUES(
         '$user_id' ,  '$type' ,  '$create_time','$title' ,  '$notice_content' ,0, '$chain_id',NULL)";
        return $sql;
    }

}